import "package:ciyue/database/app/daos.dart";
import "package:ciyue/database/app/tables.dart";
import "package:ciyue/utils.dart";
import "package:drift/drift.dart" as drift;
import "package:drift/drift.dart";
import "package:drift_flutter/drift_flutter.dart";

import "app.steps.dart";

part "app.g.dart";

AppDatabase appDatabase() {
  final connection = driftDatabase(
      name: "dictionary_list",
      native: DriftNativeOptions(databaseDirectory: databaseDirectory));
  return AppDatabase(connection);
}

@DriftDatabase(
  tables: [
    DictionaryList,
    Wordbook,
    WordbookTags,
    History,
    DictGroup,
    MddAudioList,
    MddAudioResource,
    AiExplanations,
    WritingCheckHistory,
    TranslateHistory,
    OpenRecords,
  ],
  daos: [
    DictionaryListDao,
    WordbookDao,
    WordbookTagsDao,
    HistoryDao,
    DictGroupDao,
    MddAudioListDao,
    MddAudioResourceDao,
    AiExplanationDao,
    WritingCheckHistoryDao,
    TranslateHistoryDao,
    OpenRecordsDao,
  ],
)
class AppDatabase extends _$AppDatabase {
  AppDatabase(super.e);

  @override
  MigrationStrategy get migration {
    return MigrationStrategy(
      onCreate: (Migrator m) async {
        await m.createAll();
      },
      onUpgrade: stepByStep(from1To2: (m, schema) async {
        await m.addColumn(
            schema.dictionaryList, schema.dictionaryList.fontPath);
      }, from2To3: (m, schema) async {
        await m.addColumn(
            schema.dictionaryList, schema.dictionaryList.backupPath);
      }, from3To4: (m, schema) async {
        await m.createAll();
      }, from4To5: (m, schema) async {
        await m.createTable(schema.history);
      }, from5To6: (m, schema) async {
        await m.dropColumn(schema.dictionaryList, "backup_path");
        await m.create(schema.dictGroup);
      }, from6To7: (m, schema) async {
        await m.addColumn(schema.dictionaryList, schema.dictionaryList.alias);
      }, from7To8: (m, schema) async {
        await m.addColumn(schema.wordbook, schema.wordbook.createdAt);
        await m.drop(schema.idxWordbook);
        await m.createIndex(schema.idxWordbook);
      }, from8To9: (m, schema) async {
        await m.alterTable(TableMigration(schema.wordbook));
      }, from9To10: (m, schema) async {
        await m.create(schema.mddAudioList);
        await m.create(schema.mddAudioResource);
        await m.create(schema.idxMddAudioResource);
      }, from10To11: (m, schema) async {
        await m.create(schema.aiExplanations);
        await m.createIndex(schema.idxAiExplanations);
      }, from11To12: (m, schema) async {
        await m.create(schema.writingCheckHistory);
        await m.create(schema.translateHistory);
      }, from12To13: (m, schema) async {
        await m.addColumn(schema.dictionaryList, schema.dictionaryList.type);
      }, from13To14: (m, schema) async {
        await m.addColumn(schema.dictionaryList, schema.dictionaryList.order);
      }, from14To15: (m, schema) async {
        await m.renameColumn(
            schema.dictionaryList, "alias", schema.dictionaryList.title);
      }, from15To16: (m, schema) async {
        await m.create(schema.openRecords);
        await m.create(schema.idxOpenRecords);
      }),
    );
  }

  @override
  int get schemaVersion => 16;
}
